﻿ 
using ApiDoc.Models;
using ApiDoc.Utility.Filter;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace ApiDoc.DAL
{
    [Table("api_interface")]
    public class ApiDAL : BaseDAL
    {
        public ApiDAL(DbHelper db) :base(db)
        {
            
        }

        public List<ApiModel> All()
        {
            List<ApiModel> list = new List<ApiModel>();
 
            string strSql = "select * from " + base.tableName;
            DataTable dt = db.FillTable(strSql);
            foreach (DataRow dataRow in dt.Rows)
            {
                ApiModel info = base.CreateModel<ApiModel>(dataRow);
                list.Add(info);
            }

            return list;
        }

        public List<ApiModel> Query(string title, string id)
        {
            List<ApiModel> list = new List<ApiModel>();

            string strSql = $"select api_interface.* from api_interface "
                + $"inner join api_folder on api_folder.SN = api_interface.FKSN "
                + $"where (Title like '%{title}%' or Url like '%{title}%') "
                + $"and id like '{id}%'";

            DataTable dt = db.FillTable(strSql);

            foreach (DataRow dataRow in dt.Rows)
            {
                ApiModel info = base.CreateModel<ApiModel>(dataRow);
                list.Add(info);
            }

            return list;
        }

        public string FullPath(int fksn)
        {
            try
            {
                string strSql = "with temp as";
                strSql += "(select * from api_folder where SN =" + fksn.ToString();
                strSql += "union all ";
                strSql += "select b.* from temp a inner join api_folder b on b.SN = a.ParentSN ) ";
                strSql += "select * from temp";

                DataTable dt = db.FillTable(strSql);

                string path = "";
                DataRow[] rows = dt.Select("ParentSN=0");
                if (rows.Length > 0)
                {
                    while (rows.Length > 0)
                    {
                        DataRow dataRow = rows[0];
                        path += "/" + dataRow["RoutePath"].ToString();

                        string SN = dataRow["SN"].ToString();
                        rows = dt.Select("ParentSN=" + SN);
                    } 
                }

                return path;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<ApiModel> Query(bool isStop)
        {
            List<ApiModel> list = new List<ApiModel>();
            string vStop = isStop ? "1" : "0";
            string strSql = "select * from " + base.tableName + " where isStop = " + vStop;
            DataTable dt = db.FillTable(strSql);
            foreach (DataRow dataRow in dt.Rows)
            {
                ApiModel info = base.CreateModel<ApiModel>(dataRow);
                list.Add(info);
            }

            return list;
        }
    }
}
